package com.imooc.mall.model.service.impl;

import com.imooc.mall.model.common.Constant;
import com.imooc.mall.model.dao.CartMapper;
import com.imooc.mall.model.dao.ProductMapper;
import com.imooc.mall.model.exception.ImoocMallException;
import com.imooc.mall.model.exception.ImoocMallExceptionEnum;
import com.imooc.mall.model.pojo.Cart;
import com.imooc.mall.model.pojo.Product;
import com.imooc.mall.model.service.CartService;
import com.imooc.mall.model.vo.CartVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.awt.image.ImagingOpException;
import java.util.List;

@Service
public class CartServiceImpl implements CartService {

    @Autowired
    CartMapper cartMapper;

    @Autowired
    ProductMapper productMapper;

    @Override
    public  List<CartVo> list(Integer userId){
        List<CartVo> cartVoList = cartMapper.selectList(userId);
        for (int i = 0; i < cartVoList.size(); i++) {
            CartVo cartVo=cartVoList.get(i);
            cartVo.setTotalPrice(cartVo.getPrice()*cartVo.getQuantity());
        }
        return cartVoList;
    }

    @Override
    public List<CartVo> add(Integer userId, Integer productId, Integer count){
        validProduct(productId,count);
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if(cart==null){
            cart=new Cart();
            cart.setUserId(userId);
            cart.setProductId(productId);
            cart.setQuantity(count);
            cart.setSelected(Constant.Cart.CHECKED);
            cartMapper.insertSelective(cart);
        }else{
            count=cart.getQuantity()+count;
            Cart cartNew=new Cart();
            cartNew.setId(cart.getId());
            cartNew.setUserId(cart.getUserId());
            cartNew.setProductId(cart.getProductId());
            cartNew.setQuantity(count);
            cartNew.setSelected(Constant.Cart.CHECKED);
            cartMapper.updateByPrimaryKeySelective(cartNew);
        }
        return this.list(userId);
    }
    private void validProduct( Integer productId, Integer count){
        Product product = productMapper.selectByPrimaryKey(productId);
        //判断商品是否存在，是否上下架
        if(product==null || product.getStatus().equals(Constant.SaleStatus.NOT_SALE)){
            throw new ImoocMallException(ImoocMallExceptionEnum.NOT_SALE);
        }
        if(count>product.getStock()){
            throw new ImoocMallException(ImoocMallExceptionEnum.NOT_ENOUGH);
        }
    }
    @Override
    public List<CartVo> update(Integer userId, Integer productId, Integer count){
        validProduct(productId,count);
        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if(cart==null){
           throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }else{
            Cart cartNew=new Cart();
            cartNew.setId(cart.getId());
            cartNew.setUserId(cart.getUserId());
            cartNew.setProductId(cart.getProductId());
            cartNew.setQuantity(count);
            cartNew.setSelected(Constant.Cart.CHECKED);
            cartMapper.updateByPrimaryKeySelective(cartNew);
        }
        return this.list(userId);
    }

    @Override
    public List<CartVo> delete(Integer userId, Integer productId){

        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if(cart==null){
            throw new ImoocMallException(ImoocMallExceptionEnum.DELETE_FAILED);
        }else{
            cartMapper.deleteByPrimaryKey(cart.getId());
        }
        return this.list(userId);
    }

    @Override
    public List<CartVo> select(Integer userId, Integer productId, Integer selected){

        Cart cart = cartMapper.selectCartByUserIdAndProductId(userId, productId);
        if(cart==null){
//            不在购物车里
            throw new ImoocMallException(ImoocMallExceptionEnum.UPDATE_FAILED);
        }else{
            cartMapper.selectOrNot(userId, productId, selected);
        }
        return this.list(userId);
    }

    @Override
    public List<CartVo> selectAllOrNot(Integer userId,  Integer selected){
        cartMapper.selectOrNot(userId,null,selected);
        return this.list(userId);
    }

}
